<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/28 0028
 * Time: 10:44
 */
namespace app\index\controller;
use app\admin\model\JurisdictionModel;
use app\admin\model\NoticeModel;
use app\admin\model\ProductsModel;
use app\index\model\Api as Apis;
use think\Db;
use think\Request;

class Del
{
    /**删除公告api
     * @param Request $request
     * @return \think\response\Json
     */
    public function Notice(Request $request){
        $typename = $request->param('typename',null);
        $id       = $request->param('id',null);
        $user     = session('user');
        if(!$user){
            return json(['code'=>401,'msg'=>'请先登录']);
        }
        //return json($user['jurisdiction']);
        //查看是否是部门公告或是公司公告
        if($typename=='com'){
            //公司公告检测权限是否足够
            $jurisdiction = JurisdictionModel::getJurisdiction()->where('jurisdiction_name','删除公司公告')->find();
            if($jurisdiction['amounts']>$user['jurisdiction']){
                return json(['code'=>405,'msg'=>'你的权限不够']);
            }
        }elseif ($typename=='dep'){
            //部门公告检测权限是否足够
            $jurisdiction = JurisdictionModel::getJurisdiction()->where('jurisdiction_name','删除部门公告')->find();
            $notic = NoticeModel::getNotice()->where(['notice.id'=>$id])->find();
            if(($notic['notice_type']!==$user['department_id'])||$user['jurisdiction']<$jurisdiction['amounts']){
                return json(['code'=>405,'msg'=>'你的权限不够']);
            }
        }
        //删除公告
        $bool = NoticeModel::delNotice('id',$id);
        if($bool){
            return json(['code'=>200,'msg'=>'删除成功']);
        }else{
            return json(['code'=>901,'msg'=>'删除失败']);
        }
    }

    /**删除公司产品
     * @param Request $request
     * @return array
     */
    public function products(Request $request){
        $id = $request->param('id',null);
        if(!$id){
            return returns(402,'请输入要删除的id');
        }
        //检测登录
        if(!$user = isSign()){
            return returns();
        }
        //检测权限
        if(!$bool = Apis::AuthIndex('删除公司产品')){
            return returns(403,'权限不足');
        }
        //删除公司产品
        if(ProductsModel::delProducts('id',$id)){
            return returns(200,'删除公司产品成功');
        }else{
            return returns(403,'删除公司产品失败');
        }
    }

    /**删除消息
     * @param Request $request
     * @return array
     */
    public function news(Request $request){
        $id = $request->param('id',null);
        if(!$id){
            return returns(403,'请输入要删除的id');
        }
        if(!$user=isSign()){
            return returns();
        }
        if(Db::name('news')->where('id',$id)->delete()){
            return returns(200,'删除成功');
        }else{
            return returns(403,'删除失败');
        }
    }
}